package com.atguigu.process.controller;

import com.atguigu.common.result.Result;
import com.atguigu.model.process.ProcessTemplate;
import com.atguigu.process.service.ProcessTemplateService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Api(tags="审批模板相关接口")
@RestController
@RequestMapping("/admin/process/processTemplate")
public class ProcessTemplateController {
    @Autowired
    private ProcessTemplateService processTemplateService;

    @ApiOperation("分页查询")
    @GetMapping("/{page}/{limit}")
    public Result<IPage<ProcessTemplate>> pageQuery(@PathVariable Long page,@PathVariable Long limit){
        return processTemplateService.selectPageProcessTemplate(page,limit);
    }

    @ApiOperation("根据id查询")
    @GetMapping("/get/{id}")
    public Result<ProcessTemplate> getById(@PathVariable Long id){
        return Result.ok(processTemplateService.getById(id));
    }

    @ApiOperation("新增审批模板")
    @PostMapping("/save")
    public Result save(@RequestBody ProcessTemplate processTemplate){
        processTemplateService.save(processTemplate);
        return Result.ok();
    }

    @ApiOperation("更新审批模板")
    @PutMapping("/update")
    public Result update(@RequestBody ProcessTemplate processTemplate){
        processTemplateService.updateById(processTemplate);
        return Result.ok();
    }

    @ApiOperation("根据id删除审批模板")
    @DeleteMapping("/remove/{id}")
    public Result remove(@PathVariable Long id){
        processTemplateService.removeById(id);
        return Result.ok();
    }

    @ApiOperation("上传流程定义的文件")
    @PostMapping("/uploadProcessDefinition")
    public Result uploadProcessDefinition(MultipartFile file) throws FileNotFoundException {
//        //获取classes目录的位置
//        String path = new File(ResourceUtils.getURL("classpath:")
//                .getPath()).getAbsolutePath();
        //设置上传文件夹
        File tempFile = new File("E:\\temp\\process\\");
        if(!tempFile.exists()){
            tempFile.mkdirs();
        }
        //创建空文件，实现文件写入
        String fileName = file.getOriginalFilename();
        File zipFile = new File("E:\\temp\\process\\" + fileName);
        //保存文件
        try {
            file.transferTo(zipFile);
        } catch (IOException e) {
            return Result.fail();
        }
        Map<String, Object> map = new HashMap<>();
        //根据上传地址后续部署流程定义，文件名称为流程定义的默认key
        map.put("processDefinitionPath", fileName);
        map.put("processDefinitionKey", fileName.substring(0, fileName.lastIndexOf(".")));
        return Result.ok(map);
    }

    //部署流程定义（发布流程模板）
    @ApiOperation(value = "发布")
    @GetMapping("/publish/{id}")
    public Result publish(@PathVariable Long id){
        //修改模板发布状态status，1表示已经发布
        //然后会完成流程定义的部署
        processTemplateService.publish(id);
        return Result.ok();
    }

    public static void main(String[] args) throws FileNotFoundException {
        String path = new File(ResourceUtils.getURL("classpath:")
                .getPath()).getAbsolutePath();
        System.out.println(path);
    }
}
